package lol.clann.minecraft.springboot.plugin.bukkit.impl;

import lol.clann.minecraft.springboot.api.annotation.compatibility.CompatibilityChecked;
import lol.clann.minecraft.springboot.api.annotation.condition.ConditionalOnMinecraftVersion;
import lol.clann.minecraft.springboot.api.annotation.condition.MinecraftVersionRange;
import lol.clann.minecraft.springboot.api.bukkit.model.BoundingBox;
import lol.clann.minecraft.springboot.api.bukkit.utils.BoundingBoxUtils;
import lol.clann.minecraft.springboot.bukkit.utils.BukkitUtils;
import lombok.extern.slf4j.Slf4j;
import net.minecraft.server.v1_7_R4.NBTTagCompound;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_7_R4.CraftServer;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftEntity;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author pyz
 * @date 2018/8/5 上午9:04
 */
@Slf4j
@Service
@CompatibilityChecked
@ConditionalOnMinecraftVersion(@MinecraftVersionRange(max = 10900))
public class EntityUtilsImpl_all extends EntityUtilsImpl {
    @Autowired
    private BoundingBoxUtils boundingBoxUtils;

    @Override
    public <T> T toNMS(Entity entity) {
        if (entity == null) {
            return null;
        }
        return (T) ((CraftEntity) entity).getHandle();
    }

    @Override
    public Entity toBukkit(Object nmsEntity) {
        if (nmsEntity == null) {
            return null;
        }
        return CraftEntity.getEntity((CraftServer) Bukkit.getServer(), (net.minecraft.server.v1_7_R4.Entity) nmsEntity);
    }

    /**
     * 判断盔甲架
     *
     * @param entity
     * @return
     */
    @Override
    public boolean isArmorStand(Entity entity) {
        if (version < 10800) {
//            1.8之前没有盔甲架
            return false;
        }
        return entity instanceof ArmorStand;
    }


    @Override
    public Object toNBTTagCompound(Entity entity) {
        if (entity == null) {
            return null;
        }
        Object tag = nbtUtils.newNBTTagCompound();
        if (BukkitUtils.getBukkitVersionNumber() < 10900) {
            net.minecraft.server.v1_7_R4.Entity e = toNMS(entity);
            e.e((NBTTagCompound) tag);
        } else if (BukkitUtils.getBukkitVersionNumber() < 11200) {
            net.minecraft.server.v1_9_R2.Entity e = toNMS(entity);
            e.e((net.minecraft.server.v1_9_R2.NBTTagCompound) tag);
        } else {
            net.minecraft.server.v1_12_R1.Entity e = toNMS(entity);
            e.save((net.minecraft.server.v1_12_R1.NBTTagCompound) tag);
        }
        return tag;
    }

    @Override
    public void applyNBTTagCompound(Entity entity, Object nbtTagCompound) {
        net.minecraft.server.v1_7_R4.Entity nmsEntity = toNMS(entity);
        nmsEntity.f((NBTTagCompound) nbtTagCompound);
    }

    @Override
    public Float getAbsorptionHearts(LivingEntity livingEntity) {
        net.minecraft.server.v1_7_R4.EntityLiving nms = toNMS(livingEntity);
        return nms.getAbsorptionHearts();
    }

    @Override
    public void setAbsorptionHearts(LivingEntity livingEntity, float absorptionHearts) {
        net.minecraft.server.v1_7_R4.EntityLiving nms = toNMS(livingEntity);
        nms.setAbsorptionHearts(absorptionHearts);
    }

    /**
     * 获取实体的碰撞箱
     *
     * @return
     */
    @Override
    public BoundingBox getAxisAlignedBB(Entity entity) {
        if (entity == null) {
            return null;
        }
        if (BukkitUtils.getBukkitVersionNumber() < 10800) {
            net.minecraft.server.v1_7_R4.EntityLiving nms = toNMS(entity);
            return boundingBoxUtils.fromNMS(nms.J());
        } else {
            net.minecraft.server.v1_8_R3.EntityLiving nms = toNMS(entity);
            return boundingBoxUtils.fromNMS(nms.getBoundingBox());
        }
    }

}
